﻿$(function () {
    window.progressDialog = new ProgressDialog();
    window.infoDialog = new InfoDialog();
    window.ajaxClass = new AjaxClass();
    window.resizeFlexiGrid = new ResizeFlexiGrid(".flexigrid", "#List1", ".flexigrid .bDiv", ["#TitleTR", "#TypeTR", ".flexigrid .hDiv"], 4, 4);

    $("#radio").buttonset();

    $("input[name='UserType']").change(function () {
        if (window.UserType != this.value) {
            window.UserType = this.value;
            if (window.UserType == "有效用户") {
                window.SortName = "AutoID";
                window.SortOrder = "asc";
            } else {
                window.SortName = "AutoID";
                window.SortOrder = "desc";
            }
            SetFlexiGrid();
            LoadData(this.value + "列表");
        }
        this.blur();
    });
});

function SetFlexiGrid() {
    $("#ListTD").html("<table id=\"List1\" class=\"flexme3\"></table>");
    $("#List1").flexigrid({
        colModel: [
                { display: '自动编号', name: 'AutoID', width: 50, sortable: true, align: 'left', hide: true },
                { display: '登陆名称', name: 'user_id', width: 120, sortable: true, align: 'left' },
                { display: '手机号码', name: 'mobile', width: 100, sortable: true, align: 'left' },
                { display: '电子邮件', name: 'E_mail', width: 220, sortable: true, align: 'left' },
                { display: '用户姓名', name: 'user_name', width: 80, sortable: true, align: 'left' },
                { display: '部门名称', name: 'DepartmentName', width: 80, sortable: true, align: 'left' },
                { display: '客户绑定', name: 'BindCustomer', width: 80, sortable: true, align: 'left' },
                { display: '供货商绑定', name: 'BindSupplier', width: 80, sortable: true, align: 'left' },
                { display: '操作', name: 'Operation', width: 80, sortable: false, align: 'left' }
                ],

        dataType: 'json',
        width: "auto",
        height: "auto",
        resizable: false,
        showToggleBtn: false,
        striped: true,

        sortname: window.SortName,
        sortorder: window.SortOrder,

        nonSelect: true,
        singleSelect: true,

        procmsg: '正在处理数据，请稍候……',
        nomsg: '没有符合条件的记录存在。',
        errormsg: '处理数据时出错，请与系统管理员联系！',

        onChangeSort: FlexiGrid_ChangeSort,
        onError: FlexiGrid_Error
    });
    $(".flexigrid .hDiv").bind("selectstart", function () { return false; });
    window.resizeFlexiGrid.Width();
}

/* 列表事件 --------------------------------------------------------------------------------------------*/

function FlexiGrid_Error(XMLHttpRequest, textStatus, errorThrown) {
    infoDialog.OpenDialog("处理数据时出错，请与系统管理员联系！", "Error");
}

function FlexiGrid_ChangeSort(SortName, SortOrder) {
    window.SortName = SortName;
    window.SortOrder = SortOrder;
    LoadData(window.UserType + "列表");
}

function LoadData(ActionKey) {
    var JsonSend = { "ActionParameters": JSON.stringify([[{ "OperationKey": ActionKey, "Parameters": { "@SortName": window.SortName, "@SortOrder": window.SortOrder}}]]) };
    window.ajaxClass.AjaxSubmit(JsonOriginal.UrlAction, JsonSend, "", "JsonOriginal.Operations[\"" + ActionKey + "\"].OperationData=data.ReturnData[0]; SetListData(\"List1\", \"" + ActionKey + "\");", "", false, true);
}

function SetListData(ListID, ListName) {
    window.resizeFlexiGrid.MinHeight();
    var List_Data = JsonOriginal.Operations[ListName].OperationData;
    $("#" + ListID).flexAddData(List_Data);
    $("#" + ListID + " tr td div").each(function (index, element) { if ($(element).find("*").size() == 0) { $(element).attr("title", $(element).html() == "&nbsp;" ? "" : $(element).text()) } });
    window.resizeFlexiGrid.Height();
    $("#List1 td div[title='Administrator']").parent().parent().find("td:eq(6),td:eq(7),td:eq(8)").html(" ");
}

/* 初始化 --------------------------------------------------------------------------------------------*/

$(function () {
    window.UserType = "有效用户";
    window.SortName = "AutoID";
    window.SortOrder = "asc";
    SetFlexiGrid();
    SetListData("List1", "有效用户列表");
    window.resizeFlexiGrid.Resize("resizeFlexiGrid");
});